package com.youmu.sichuansfc.controller;

import com.youmu.sichuansfc.common.CommonRes;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author WYZ
 * @create 2018-03-30 15:26
 * @desc
 **/

public class BaseController extends HttpServlet{

    protected Logger errorlog = Logger.getLogger(this.getClass());

    public BaseController(){

    }

    public void getUrl(HttpServletRequest request)throws Exception{
        errorlog.info(request.getRequestURI());
    }


    /**
     *  全局 捕获controller 异常
     * @param request
     * @param e
     */
    @ExceptionHandler
    public @ResponseBody
    CommonRes exception(HttpServletRequest request, Exception e){
        final String uri = request.getRequestURI();
        final Map params = request.getParameterMap();

        e.printStackTrace();
        errorlog.info("------------------------------------------");
        errorlog.info("url = " + uri);
        errorlog.info("params : "+ new JSONObject(params));
        errorlog.info(e.getMessage());
        errorlog.info("------------------------------------------");

        return CommonRes.fail();
    }
}
